package com.meme.editor;

public class Box2d 
{
	public int left;
	public int top;
	public int right;
	public int bottom;
	
	public Box2d ()
	{
		this (0, 0, 0, 0);
	}
	
	public Box2d (int l, int t, int r, int b)
	{
		set (l, t, r, b);
	}
	
	public void set (int l, int t, int r, int b)
	{
		left = l < r ? l : r;
		top = t < b ? t : b;
		right = l < r ? r : l;
		bottom = t < b ? b : t;	
	}
	
	public void reset ()
	{
		set (0, 0, 0, 0);
	}
	
	public void scale (int scale)
	{
		left *= scale;
		right *= scale;
		top *= scale;
		bottom *= scale;
	}
	
	public void shift (int dx, int dy)
	{
		left += dx;
		right += dx;
		top += dy;
		bottom += dy;	
	}
	
	public void add (Box2d another)
	{
		left += another.left;
		right += another.right;
		top += another.top;
		bottom += another.bottom;	
	}
	
	public boolean intersectWith (Box2d another)
	{
		return left < another.right
			&& right > another.left
			&& top < another.bottom
			&& bottom > another.top;
	}
	
	public boolean containsPoint (int x, int y)
	{
		return x >= left
			&& x < right
			&& y >= top
			&& y < bottom;
	}
	
	public boolean containsBox (Box2d anotherBox)
	{
		return left <= anotherBox.left
		    && right >= anotherBox.right
		    && top <= anotherBox.top
		    && bottom >= anotherBox.bottom;
	}
	
	public void copyFrom (Box2d another)
	{
		set(another.left, another.top, another.right, another.bottom);
	}
	
	public void copyTo (Box2d another)
	{
		if (another != null)
		{
			another.set(left, top, right, bottom);
		}
	}
	
	public void adjust (boolean flipX, boolean flipY)
	{

		if (flipX)
		{
			int temp = left;
			left = -right;
			right = -temp;
		}
		
		if (flipY)
		{
			int temp = top;
			top = -bottom;
			bottom = -temp;
		}

	}
}
